//
//  TitleBarView.m
//  OSClient
//
//  Created by Chaosky on 15/10/20.
//  Copyright © 2015年 1000phone. All rights reserved.
//

#import "TitleBarView.h"
#import "Utils.h"

// 按钮开始的tag值
#define BUTTON_BEGIN_TAG 100

@implementation TitleBarView
{
    NSArray * _titleArray; // 保存标题数组
    NSMutableArray * _titleButtonArray; // 标题按钮数组
    NSInteger _currentIndex; // 当前选中按钮
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

- (instancetype)initWithFrame:(CGRect)frame titles:(NSArray *)titleArray
{
    if (self = [super initWithFrame:frame]) {
        
        // 将传入的数组赋给_titleArray
        _titleArray = [NSArray arrayWithArray:titleArray];
        
        [self setupUI];
    }
    return self;
}

// 创建UI
- (void)setupUI
{
    _titleButtonArray = [[NSMutableArray alloc] init];
    // 获取当前scrollView的大小
    CGSize scrollViewSize = self.frame.size;
    
    // 设置contentSize
    self.contentSize = scrollViewSize;
    
    // 计算每个按钮的大小
    CGFloat buttonWidth = scrollViewSize.width / _titleArray.count;
    CGFloat buttonHeight = scrollViewSize.height;
    
    // 遍历数组，创建UIButton，一个挨着一个
    [_titleArray enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL * stop) {
        UIButton * button = [UIButton buttonWithType:UIButtonTypeCustom];
        button.frame = CGRectMake(idx*buttonWidth, 0, buttonWidth, buttonHeight);
        [button setTitle:obj forState:UIControlStateNormal];
        [button setTitleColor:[UIColor titleBarColor] forState:UIControlStateNormal];
        [button setTitleColor:[UIColor orangeColor] forState:UIControlStateSelected];
        // 设置Tag值
        button.tag = BUTTON_BEGIN_TAG + idx;
        // 100~n
        // 添加点击事件
        [button addTarget:self action:@selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];
        
        [self addSubview:button];
        [_titleButtonArray addObject:button];
    }];
    // 将当前的按钮置为第一个
    _currentIndex = BUTTON_BEGIN_TAG;
    ((UIButton *)_titleButtonArray[0]).selected = YES;
}

// 按钮点击事件
- (void)buttonPressed:(UIButton *) button
{
    // 判断点击的按钮是否已点击
    if (_currentIndex != button.tag) {
        // 找到前一个已被选中的按钮
        UIButton * preBtn = (id)[self viewWithTag:_currentIndex];
        preBtn.selected = NO;
        
        // 将当前按钮的选中状态设置为YES
        button.selected = YES;
        
        // 修改当前选中按钮
        _currentIndex = button.tag;
        
        // block传值
        if (self.block) {
            self.block(_currentIndex - BUTTON_BEGIN_TAG);
        }
        // 视图形变
        button.transform = CGAffineTransformMakeScale(1.2, 1.2);
        // 叠加形变效果
        //button.transform = CGAffineTransformRotate(button.transform, - M_PI_2);
        // 恢复前一个按钮的形状
        preBtn.transform = CGAffineTransformIdentity;
    }
}

- (void)scrollToIndex:(NSInteger)index
{
    // 找到将要滚动的位置index对应的UIButton
    UIButton * nextBtn = (id)[self viewWithTag:BUTTON_BEGIN_TAG + index];
    if (nextBtn) {
        [self buttonPressed:nextBtn];
    }
}

@end



